package altocloud.ehcache.test;


import java.util.concurrent.TimeUnit;

public class TimeMeasurement  {
    private long startTime;
    private double duration;
    private TimeUnit timeUnit;
    private int samples;
    
    public TimeMeasurement(TimeUnit timeUnit) {
        this(timeUnit, 1);
    }
    
    public TimeMeasurement(TimeUnit timeUnit, int samples) {
        this.timeUnit = timeUnit;
        this.samples = samples;
    }
    
    public void startMeasurement() {
        startTime = now();
    }
    
    public void endMeasurement() {
        duration = ((double) (now() - startTime)) / ((double) samples);
    }
    
    public double getTime() {
       return duration;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    private long now() {
        if (timeUnit == TimeUnit.MILLISECONDS) {
            return System.currentTimeMillis();
        } else if (timeUnit == TimeUnit.NANOSECONDS) {
            return System.nanoTime();
        } else {
            throw new UnsupportedOperationException("TimeUnit not supported:" + timeUnit);
        }
    }
}

